﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLySim.BLO;

namespace QuanLySim
{
    public partial class frmTimKiem : Form
    {
        #region variable
        private clsTimKiemBLO m_TimKiemBlo;
        private string m_strTenMang;
        #endregion

        #region mothod
        public frmTimKiem()
        {
            InitializeComponent();
            m_TimKiemBlo = new clsTimKiemBLO();
        }

        public frmTimKiem(string strTenMang)
        {
            m_strTenMang = strTenMang;
            InitializeComponent();
            m_TimKiemBlo = new clsTimKiemBLO();
        }

        private void CbxTenGoi()
        {
            DataTable Table = new DataTable();
            Table = m_TimKiemBlo.TenGoi();
            if (Table != null)
            {
                //this.cbxTenGoi.Text = Table.Rows[0][0].ToString();
                this.cbxTenGoi.Items.Add("(None)");
                foreach (DataRow row in Table.Rows)
                {
                    this.cbxTenGoi.Items.Add(row[0].ToString());
                }
            }
            else
            {
                MessageBox.Show("Lỗi: " + m_TimKiemBlo.ERORR, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private int DataGridViewTimKiem()
        {
            int iDem = 0;
            DataTable Table = new DataTable();
            Table = m_TimKiemBlo.TimKiem();
            if (Table != null)
            {
                this.dtgvDanhSach.DataSource = Table;
                iDem = Table.Rows.Count;
            }
            else
            {
                MessageBox.Show("Lỗi: " + m_TimKiemBlo.ERORR, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return iDem;
        }

        private string DauSo(string strSoDienThoai)
        {
            string strSoThu2 = strSoDienThoai.Substring(1, 1);
            if (int.Parse(strSoThu2) == 9)//so dt 10 so
            {
                return strSoDienThoai.Substring(0, 3);//tra ve 3 so dau
            }
            else//so dt thoai 11 so
            {
                return strSoDienThoai.Substring(0, 4);//tra ve 4 so dau
            }
        }

        private void ChonHinh()
        {
            switch (m_strTenMang)
            {
                case "Viettel": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.Viettel; break;
                case "Mobifone": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.mobilfone; break;
                case "Vinaphone": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.Vinafone; break;
                case "Vietnamobile": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.Vietnammobile; break;
                case "Beeline": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.beline; break;
                case "Sfone": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.sfone; break;
                case "EVNTelecom": this.ptbxHinhLogo.BackgroundImage = global::QuanLySim.Properties.Resources.evn; break;
            }
        }
        #endregion

        #region su kien
        private void frmTimKiem_Load(object sender, EventArgs e)
        {
            this.lblLoGo.Text = m_strTenMang;
            ChonHinh();
            CbxTenGoi();

            m_TimKiemBlo = new clsTimKiemBLO();
            m_TimKiemBlo.TIMKIEMDTO.TENMANG = m_strTenMang;

            int iSoLuong = DataGridViewTimKiem();
            this.lbtnTongSoSim.Text += iSoLuong.ToString();
        }

        private void btnDatMua_Click(object sender, EventArgs e)
        {
            frmThanhToan TT = new frmThanhToan();
            TT.ShowDialog();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            m_TimKiemBlo = new clsTimKiemBLO();
            m_TimKiemBlo.TIMKIEMDTO.TENMANG = m_strTenMang;
            if (this.chbxDauSo.Checked == true && this.txtSoDienThoai.Text != "")
            {
                m_TimKiemBlo.TIMKIEMDTO.DAUSO = DauSo(this.txtSoDienThoai.Text);
            }
            else
            {
                m_TimKiemBlo.TIMKIEMDTO.SODIENTHOAI = this.txtSoDienThoai.Text;
            }
            m_TimKiemBlo.TIMKIEMDTO.TENGOI = "";
            if (this.cbxTenGoi.SelectedIndex >= 0)
            {
                m_TimKiemBlo.TIMKIEMDTO.TENGOI = this.cbxTenGoi.SelectedItem.ToString();
            }
            if (m_TimKiemBlo.TIMKIEMDTO.TENGOI == "(None)")
            {
                m_TimKiemBlo.TIMKIEMDTO.TENGOI = "";
            }
            m_TimKiemBlo.TIMKIEMDTO.GIA = this.txtGiaCa.Text;
            if (rbtn10So.Checked == true)
            {
                m_TimKiemBlo.TIMKIEMDTO.LOAISIM = "10 Số";
            }
            if (rbtn11So.Checked == true)
            {
                m_TimKiemBlo.TIMKIEMDTO.LOAISIM = "11 Số";
            }
            
            int iSoLuong = DataGridViewTimKiem();
            this.lbtnTongSoSim.Text += iSoLuong.ToString();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Có chắc muốn thoát ?", "Lưu ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                this.Close();
            }
        }
        #endregion

    }
}